package com.xframe.xdal.core.util.word;

import com.xframe.xdal.core.util.pdf.BarChartOpt;
import com.xframe.xdal.core.util.pdf.LineChartOpt;
import com.xframe.xdal.core.util.pdf.PieChartOpt;
import com.xframe.xdal.core.util.pdf.TableCol;
import lombok.extern.log4j.Log4j2;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collection;


public class WordBuilder {

    private XWPFDocument document;

    public XWPFDocument getDocument(){
        return this.document;
    }

    /**
     * 文档作者
     */
    private String author;
    /**
     * 文档创建者
     */
    private String creator;
    /**
     * 文档保存路径
     */
    private String path;

    public WordBuilder init(){
        document = new XWPFDocument();
        return this;
    }

    private WordBuilder(String author,String creator,String path){
        this.author = author;
        this.creator = creator;
        this.path = path;
        // Rectangle A4 = new RectangleReadOnly(595.0F, 842.0F);
        // new RectangleReadOnly(842.0F, 1191.0F);

    }

    public static WordBuilder create(String author,String creator,String path){
        WordBuilder wDocument = new WordBuilder(author,creator,path);
        return wDocument;
    }


    /**
     * 设置图片
     *
     * @param imgPath 图片路径
     * @param width 图片宽度
     * @param height 图片高度
     * @return this
     */
    public WordBuilder setImage(String imgPath, int width, int height) throws IOException, InvalidFormatException {
        return setImage(imgPath, 2,width,height);
    }
    /**
     * 设置图片
     *
     * @param imgPath 图片路径
     * @return this
     */
    public WordBuilder setImage(String imgPath) throws IOException, InvalidFormatException {
        return setImage(imgPath,2,500,300);
    }

    /**
     * 设置图片
     *
     * @param imgPath 图片路径
     * @param alignment 对齐方式
     * @param width 图片宽度
     * @param height 图片高度
     * @return this
     */
    public WordBuilder setImage(String imgPath,int alignment,int width,int height)
            throws IOException, InvalidFormatException {
        XWPFParagraph paragraph = document.createParagraph();
        paragraph.setAlignment(ParagraphAlignment.valueOf(alignment));
        XWPFRun run = paragraph.createRun();

        int imgFormat = XWPFDocument.PICTURE_TYPE_JPEG;
        run.addPicture(getClass().getResourceAsStream(imgPath),
                imgFormat, "", Units.toEMU(width), Units.toEMU(height));

        return this;
    }


    /**
     * 生成折线图
     *
     * @param title 数据
     * @param xLabel X轴
     * @param yLabel Y轴
     * @param values 数据
     * @return this
     */
    public WordBuilder setLineChart(String title,
                                    String xLabel, String yLabel, Collection values, String groupName
            , String keyName, String valueName) throws IOException {


        LineChartOpt lineChartOpt = LineChartOpt.buildLineChartOpt(title,xLabel,yLabel,values,groupName,keyName,valueName);
        byte[] pictureData = lineChartOpt.buildLineChartImageByteArray();
        setChar(pictureData);
        return this;
    }

    private void setChar(byte[] pictureData){
        try {
            XWPFParagraph paragraph = document.createParagraph();
            XWPFRun run = paragraph.createRun();
            int imgFormat = XWPFDocument.PICTURE_TYPE_JPEG;
            run.addPicture(new ByteArrayInputStream(pictureData), document.getNextPicNameNumber(imgFormat),
                    "image description", Units.toEMU(450), Units.toEMU(300));
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }

    /**
     * 生成堆叠柱状图
     *
     * @param title 数据
     * @param xLabel X轴
     * @param yLabel Y轴
     * @param values 数据
     * @return this
     */
    public WordBuilder setBarChart(String title,
                                   String xLabel, String yLabel, Collection values,String groupName
            ,String keyName,String valueName) throws IOException {


        BarChartOpt barChartOpt = BarChartOpt.buildBarChartOpt(false,title,xLabel,yLabel,values,groupName,keyName,valueName);
        byte[] pictureData = barChartOpt.buildBarChartImageByteArray();
        setChar(pictureData);
        return this;
    }

    public WordBuilder setStackedBarChart(String title,
                                          String xLabel, String yLabel, Collection values,String groupName
            ,String keyName,String valueName) throws IOException {
        BarChartOpt barChartOpt = BarChartOpt.buildBarChartOpt(true,title,xLabel,yLabel,values,groupName,keyName,valueName);
        byte[] pictureData = barChartOpt.buildBarChartImageByteArray();
        setChar(pictureData);
        return this;

    }

    /**
     * 生成堆叠柱状图
     *
     * @param values 数据
     * @param keyName 数据
     * @param valueName 数据
     * @return this
     */
    public WordBuilder setPieChart(Collection values,boolean percentageFlag,String keyName,String valueName) throws IOException {
        PieChartOpt pieChartOpt = PieChartOpt.buildPieChartOpt(values,percentageFlag,keyName,valueName);
        byte[] pictureData = pieChartOpt.buildPieChartImageByteArray();
        setChar(pictureData);
        return this;
    }

    /**
     * 插入段落 加粗 居中 24号字体 宋体
     * @param title 段落的文本
     */
    public WordBuilder setHead(String title){
        return setHead(title, 1,2,24);
    }
    /**
     * 插入段落
     * @param title 段落的文本
     * @param style 0 不加粗 1 加粗
     * @param align 对齐方式 1 left 2 CENTER 3 RIGHT 4 BOTH 5 MEDIUM_KASHIDA 6 DISTRIBUTE
     * @param fontSize 字体大小
     */
    public WordBuilder setHead(String title, int style, int align, int fontSize) {
        setHead(title,style,align,fontSize,"宋体",5);
        return this;
    }
    /**
     * 插入段落
     * @param title 段落的文本
     * @param style 0 不加粗 1 加粗
     * @param align 对齐方式 1 left 2 CENTER 3 RIGHT 4 BOTH 5 MEDIUM_KASHIDA 6 DISTRIBUTE
     * @param fontSize 字体大小
     * @param fontFamily 默认宋体
     * @param textPosition 行间距
     */
    public WordBuilder setHead(String title, int style, int align, int fontSize
            ,String fontFamily,int textPosition) {
        createParagraph(title,style,align,fontSize,fontFamily,textPosition);
        return this;
    }

    /**
     * 插入空白行
     */
    public WordBuilder blankline(){
        XWPFParagraph paragraph = document.createParagraph();
        XWPFRun run = paragraph.createRun();
        run.setText("");
        return this;
    }

    /**
     * 插入段落
     * @param content 段落的文本
     * @param style 0 不加粗 1 加粗
     * @param alignment 对齐方式 1 left 2 CENTER 3 RIGHT 4 BOTH 5 MEDIUM_KASHIDA 6 DISTRIBUTE
     * @param fontSize 字体大小
     * @param fontFamily 默认宋体
     * @param textPosition 行间距
     */
    private void createParagraph(String content, int style, int alignment,
                                 int fontSize,String fontFamily,int textPosition){
        if(document != null && content != null){
            // 创建段落
            XWPFParagraph paragraph = document.createParagraph();
            XWPFRun run = paragraph.createRun();
            run.setText(content);

            ParagraphAlignment paragraphAlignment = ParagraphAlignment.valueOf(alignment);
            //对齐方式
            paragraph.setAlignment(paragraphAlignment);
            //是否加粗
            if(style == 1) {
                run.setBold(true);
            }
            //字体颜色 run.setColor(color);
            //字体大小
            run.setFontSize(fontSize);
            //字体
            if(fontFamily == null || fontFamily.equals("")){
                run.setFontFamily("宋体");
            }else {
                run.setFontFamily(fontFamily);
            }
            //行间距
            run.setTextPosition(textPosition);
        }
    }



    /**
     * 表格
     *
     * @param tableColList 表头
     * @param values 数据
     */
    public WordBuilder setTable(java.util.List<TableCol> tableColList,
                                Collection values){

        WordTableOpt wordTableOpt = new WordTableOpt(this,tableColList,values,true);
        return setTable(wordTableOpt);
    }

    /**
     * 表格
     *
     * @param wordTableOpt 表格设定对象
     */
    public WordBuilder setTable(WordTableOpt wordTableOpt){
        wordTableOpt.buildTabel();
        return this;
    }

    /**
     * 保存到路径磁盘
     */
    public void saveToPath() throws IOException {
        //File file = new File(this.path);
        FileOutputStream out = new FileOutputStream(this.path);
        document.write(out);
        out.close();
        document.close();

    }

}
